package com.tc.data.spring.util;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class DatasourceUtils {

	public static void validateConnection(DataSource dataSource) throws SQLException {
		Connection connection=null;
		PreparedStatement stmt = null;
		try {
			connection = dataSource.getConnection();
			stmt = connection.prepareStatement("SELECT 1 FROM dual");
			stmt.executeQuery();
		} catch (SQLException e) {
			throw e;
		}finally{
			close(stmt, connection);
		}
	}

	 public static  void close( Statement st, Connection conn) throws SQLException {
            try {
                if (st != null) {
                    st.close(); // 关闭Statement
                }
            } finally {
                if (conn != null) {
                    conn.close(); // 关闭连接
                }
            }
	    }
}
